/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BasicFrame.java
 *
 * Created on Oct 26, 2009, 9:53:17 AM
 */

/**
 *
 * @author annawu
 */

import java.awt.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import javax.swing.JScrollPane;

import javax.swing.BoxLayout;

import org.jdesktop.layout.GroupLayout;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.DateTickUnitType;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.time.TimeSeriesCollection;


import com.toedter.calendar.JDateChooser;

public class BasicFrame extends javax.swing.JFrame {
	  // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    
    private javax.swing.JPanel jPanel7; 
    
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel jSelectedTagLabel;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JButton jUpdateButton;
    // End of variables declaration//GEN-END:variables
    private JDateChooser startDateControl;
    private JDateChooser endDateControl;
    private MyChartMouseListener listener;
    private JFreeChart chart1;
    private JFreeChart chart2;
    private JFreeChart chart3;

    public AggregateDemo ad = new AggregateDemo();

	public TagSearchPanel tsp = new TagSearchPanel();
	
	
	public TextField entry = new TextField();
    public Button searchBt = new Button("Search");
	public Button clearBt = new Button("Clear");
	
    /** Creates new form BasicFrame */
    public BasicFrame() {
 //   	initSize();
        initComponents();
        initCharts();
    

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    /**
     * 
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    
/*    private static void initSize() {
    	java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    } */ 
    
    
    private void initComponents() {

    	
        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        
//        ArrayList<String> myArr = new ArrayList<String>();
//        myArr.add("http://www.gstatic.com/hostedimg/ef3fe9b49b289ee2_landing");
//        myArr.add("http://www.iacuc.arizona.edu/training/cats/images/Tabby1-DomesticCat-Closeup.jpg");
//        myArr.add("http://www.gstatic.com/hostedimg/ef3fe9b49b289ee2_landing");
//        myArr.add("http://www.iacuc.arizona.edu/training/cats/images/Tabby1-DomesticCat-Closeup.jpg");
//        myArr.add("http://www.gstatic.com/hostedimg/ef3fe9b49b289ee2_landing");
//        myArr.add("http://www.iacuc.arizona.edu/training/cats/images/Tabby1-DomesticCat-Closeup.jpg");
        
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jSelectedTagLabel = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jComboBox1 = new javax.swing.JComboBox();
        jPanel6 = new javax.swing.JPanel();
        jUpdateButton = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        
        jPanel7 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("SNV"));
        jPanel1.setAutoscrolls(true);
        jPanel1.setInheritsPopupMenu(true);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 455, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, java.awt.Toolkit.getDefaultToolkit().getScreenSize().height-280, Short.MAX_VALUE)
        );
        
       jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder("Search Area"));
       jPanel7.setAutoscrolls(true);
       jPanel7.setInheritsPopupMenu(true);
       org.jdesktop.layout.GroupLayout jPanel7Layout = new org.jdesktop.layout.GroupLayout(jPanel7);
       
        
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Pictures"));

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        jSelectedTagLabel.setText("Please click to select tag.");

//        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
//        jPanel3.setLayout(jPanel3Layout);
//        jPanel3Layout.setHorizontalGroup(
//            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
//            .add(jPanel3Layout.createSequentialGroup()
//                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 104, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
//                .add(18, 18, 18)
//                .add(jSelectedTagLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 221, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
//                .addContainerGap(21, Short.MAX_VALUE))
//        );
//        jPanel3Layout.setVerticalGroup(
//            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
//            .add(jPanel3Layout.createSequentialGroup()
//                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
//                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
//                    .add(jSelectedTagLabel))
//                .addContainerGap(37, Short.MAX_VALUE))
//        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Control Panel"));

        jCheckBox1.setText("jCheckBox1");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        org.jdesktop.layout.GroupLayout jPanel6Layout = new org.jdesktop.layout.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 190, Short.MAX_VALUE)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 24, Short.MAX_VALUE)
        );

        jUpdateButton.setText("Update");

        org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel4Layout.createSequentialGroup()
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jUpdateButton))
                    .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel4Layout.createSequentialGroup()
                            .add(146, 146, 146)
                            .add(jPanel6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .add(jPanel4Layout.createSequentialGroup()
                            .addContainerGap()
                            .add(jCheckBox1)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 193, Short.MAX_VALUE)
                            .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .add(28, 28, 28))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jCheckBox1)
                    .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 23, Short.MAX_VALUE)
                .add(jPanel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jUpdateButton)
                .add(6, 6, 6))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Timeline"));

        org.jdesktop.layout.GroupLayout jPanel5Layout = new org.jdesktop.layout.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 455, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 136, Short.MAX_VALUE)
        );

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 375, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 313, Short.MAX_VALUE)
        );

        //jTabbedPane1.addTab("Tags", jPanel2);
        
        TagSearchPanel tpanel = new TagSearchPanel();
        jTabbedPane1.addTab("tabN", tpanel);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    
                    .add(jPanel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel7, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        ));
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 341, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel7, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)                           
                .add(jPanel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
   
 
            .add(layout.createSequentialGroup()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        
       

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
         //       new BasicFrame().setVisible(true);
            	BasicFrame frame = new BasicFrame();
            	frame.validate();
            	frame.setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
            	frame.setResizable(true);
            	frame.validate();
            	frame.setVisible(true);

            }
        });
    }

  
    
    
    
    private void initCharts(){
    	//miao test place start
    	//AggregateDemo ad = new AggregateDemo();   
    	//ad.setLocation(3, 15);
    	//jPanel1.add(ad);
    	
    	generateSNV();
    	
        generateTagSearchPanel();
    	
        listener = new MyChartMouseListener(this.jSelectedTagLabel);
        generateTagTab();//generate tag tab
        generateBarChart();//generate bar chart
        generatePieChart();//generate pie chart
        

        generateTimeLine();//generate time line panel (set to jPanel5)
        generateCalenderChooser();
   
        generatePicturegrid();
        
        //miao test place over
    }
    private void generatePicturegrid(){
    	 ArrayList<String> myArr = new ArrayList<String>();
       myArr.add("http://www.gstatic.com/hostedimg/ef3fe9b49b289ee2_landing");
       myArr.add("http://www.iacuc.arizona.edu/training/cats/images/Tabby1-DomesticCat-Closeup.jpg");
       myArr.add("http://www.gstatic.com/hostedimg/ef3fe9b49b289ee2_landing");
       myArr.add("http://www.iacuc.arizona.edu/training/cats/images/Tabby1-DomesticCat-Closeup.jpg");
       myArr.add("http://www.gstatic.com/hostedimg/ef3fe9b49b289ee2_landing");
       myArr.add("http://www.iacuc.arizona.edu/training/cats/images/Tabby1-DomesticCat-Closeup.jpg");
    	Picturegrid pictures=new Picturegrid(myArr);
        jPanel3.add(pictures);
    }
    
    
    private void generateSNV() {
    	ad.setLocation(3, 15);
        org.jdesktop.layout.GroupLayout jPanel1Layout = (GroupLayout) jPanel1.getLayout();
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(ad, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                    .add(ad, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    )
            ); 
        ad.addMouseListener(new MouseAdapter(){
        	 public void mouseClicked(MouseEvent e) {
        		 	e.consume();
        		 	jPanel3.removeAll();
        		 	Picturegrid pictures=new Picturegrid(ad.picturelist);
        		 	jPanel3.add(pictures);
        		 	jPanel3.validate();
        		 	String user_id= ad.user_id;
        		 	updateCharts(user_id);
        		// 	System.out.println(e.getClickCount());
        	    }

        });
        
        jPanel1.add(ad);
    }
    

    
    private void generateTagTab(){
     	JScrollPane scrollingTags = new JScrollPane(ad.tagText);
    	jTabbedPane1.addTab("Tags", scrollingTags);
    }   

    
    private void generateTagSearchPanel() {
    	

    //	tsp.setLocation(3, 15);
  /*      org.jdesktop.layout.GroupLayout jPanel7Layout = (GroupLayout) jPanel7.getLayout();
        jPanel7Layout.setHorizontalGroup(
                jPanel7Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(tsp, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
        jPanel7Layout.setVerticalGroup(
                jPanel7Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel7Layout.createSequentialGroup()
                    .add(tsp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    )
            ); */

/*
       	tsp.searchBt.addMouseListener(new MouseAdapter(){
       	 public void mouseClicked(MouseEvent e) {
       		 	e.consume();
       		 	jPanel3.removeAll();
       		 	//tsp.search();
       		 	Picturegrid pictures=new Picturegrid(tsp.picturelist2);
       		 	jPanel3.add(pictures);
       		 	jPanel3.validate();
       		// 	System.out.println(e.getClickCount());
       	    }
       	});
  */     

       // jPanel7.add(tsp);
        
    	jPanel7.add(entry);
    	jPanel7.add(searchBt);
    	jPanel7.add(clearBt);
    	entry.setVisible(true);
		entry.setEditable(true);
		entry.setText("Enter Query Tag\n");	
		    	
    	clearBt.addActionListener(new ActionListener() {
			 public void actionPerformed(ActionEvent e) {
				  tsp.tList.clear();
			      tsp.s = "" ;
			      entry.setText(tsp.s);
			      //disp.setText(tsp.s);
	 		      // Re-initialize the display to original - Call the First function 
			}			
		 });
    			
    	
    
		searchBt.addActionListener(new ActionListener(){
		
			public void actionPerformed(ActionEvent e) {
	       		 String temp = entry.getText();
			      if (temp == null)
			    	  return;
			      
			      tsp.tList.add(temp) ;
			      tsp.s = tsp.s + ',' + temp;
			      //disp.setText(s);
			      entry.setText("");
			      
			      System.out.print(tsp.tList);
			     try{ 
	       		 tsp.search();
	       		 System.out.print("inside the try");
			     }
			     catch (Exception err1)
			      {
			    	 System.out.print("nothing happened");
			    	  System.err.println(err1.getMessage());
			    			    	 
			     }
	       		 //e.consume();
	       		 	jPanel3.removeAll();
	       		 	
	       		 	Picturegrid pictures=new Picturegrid(tsp.picturelist2);
	       		 	jPanel3.add(pictures);
	       		 	jPanel3.validate();
	       		// 	System.out.println(e.getClickCount());
		}    	    
		});
    }       
    	
    
    private void generateBarChart(){
    	DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        Iterator<TagUsage> it = StatisticHelper.getTagsUsage().iterator();
        while(it.hasNext()){
        	TagUsage e = it.next();
        	dataset.addValue(e.getUsage(), e.getName(), "Tag");
        }
//        dataset.addValue(1.0, "Row 1", "Column 1");
//        dataset.addValue(5.0, "Row 1", "Column 2");
//        dataset.addValue(3.0, "Row 1", "Column 3");
//        dataset.addValue(2.0, "Row 2", "Column 1");
//        dataset.addValue(3.0, "Row 2", "Column 2");
//        dataset.addValue(2.0, "Row 2", "Column 3");
        
        chart1 = ChartFactory.createBarChart(
        	    "Bar Chart", // chart title
        	    "", // domain axis label
        	    "Usage", // range axis label
        	    dataset, // data
        	    PlotOrientation.VERTICAL, // orientation
        	    true, // include legend
        	    true, // tooltips?
        	    false // URLs?
        	);
        ChartPanel chartPanel1 = new ChartPanel(chart1, false);
        chartPanel1.setPreferredSize(new Dimension(250, 135));
        chartPanel1.addChartMouseListener(listener);
        jTabbedPane1.addTab("Bar Chart",chartPanel1);
    }
    
    private void generatePieChart(){
    	DefaultPieDataset dataset2 = new DefaultPieDataset();
//      dataset2.setValue("One", new Double(43.2));
//      dataset2.setValue("Two", new Double(10.0));
//      dataset2.setValue("Three", new Double(27.5));
//      dataset2.setValue("Four", new Double(17.5));
//      dataset2.setValue("FiveTest", new Double(11.0));
//      dataset2.setValue("Six", new Double(19.4));
      Iterator<TagUsage> it2 = StatisticHelper.getTagsUsage().iterator();
      while(it2.hasNext()){
      	TagUsage e = it2.next();
      	dataset2.setValue(e.getName(), e.getUsage());
      }
      chart2 = ChartFactory.createPieChart(
              "Pie Chart",  // chart title
              dataset2,     // data
              true,         // include legend
              true,
              false
          );
      PiePlot plot = (PiePlot) chart2.getPlot();
      plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
      plot.setNoDataMessage("No data available");
      plot.setCircular(false);
      plot.setLabelGap(0.02);
      ChartPanel chartPanel2 = new ChartPanel(chart2, false);
      chartPanel2.setPreferredSize(new Dimension(250, 135));
      chartPanel2.addChartMouseListener(listener);
      jTabbedPane1.addTab("Pie Chart",chartPanel2);
    }

    private void generateTimeLine(){  	
    	TimeSeriesCollection data2 = new TimeSeriesCollection(StatisticHelper.getImageSeries());
 
    	chart3 = ChartFactory.createTimeSeriesChart("", "", "Count", data2, false, true, false);
        XYPlot plot2 = chart3.getXYPlot();
        plot2.setBackgroundPaint(Color.lightGray);
        plot2.setDomainGridlinePaint(Color.white);
        plot2.setRangeGridlinePaint(Color.white);
        //plot2.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
        plot2.setDomainCrosshairVisible(true);
        plot2.setRangeCrosshairVisible(true);
        
        Date start = new Date(104, 0, 1);
    	Date end = new Date(110,0,1);
        DateAxis axis = (DateAxis)plot2.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("MM/yy"));;
        axis.setAutoRange(true);
        axis.setRange(start, end);
        axis.setTickUnit(new DateTickUnit(DateTickUnitType.MONTH, 6, new SimpleDateFormat("MM/yy")));
        axis.setLabelAngle(0.25 * Math.PI);
        axis.setVerticalTickLabels(true);
                     
        XYItemRenderer renderer2 = plot2.getRenderer();
        renderer2.setBaseToolTipGenerator(new StandardXYToolTipGenerator(
                StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
                new SimpleDateFormat("MM/yy"), new DecimalFormat("0.00")));
        renderer2.setSeriesItemLabelsVisible(0, true);
        ChartPanel chartPanel3 = new ChartPanel(chart3, false);
        chartPanel3.setPreferredSize(new java.awt.Dimension(450, 150));       
        chartPanel3.setMouseZoomable(true, false);
        
        //update jPanel to this chart Panel
        //jPanel5 = (chartPanel3); 
        org.jdesktop.layout.GroupLayout jPanel5Layout = (GroupLayout) jPanel5.getLayout();
        jPanel5Layout.setHorizontalGroup(
                jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(chartPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
            jPanel5Layout.setVerticalGroup(
                jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel5Layout.createSequentialGroup()
                    .add(chartPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    )
            );
        
    }
    
    private void generateCalenderChooser(){
    	startDateControl = new JDateChooser(new Date(104, 0, 1));
        endDateControl = new JDateChooser(new Date(110,0,1));
        
        jUpdateButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
            	updateActionPerformed(evt);
            }
        });       
        jPanel6.setLayout(new BoxLayout(jPanel6, BoxLayout.X_AXIS));
        startDateControl.setAlignmentX(Component.CENTER_ALIGNMENT);
        endDateControl.setAlignmentX(Component.CENTER_ALIGNMENT);
        jPanel6.add(startDateControl);
        jPanel6.add(endDateControl);
        
    }
    
    private void updateActionPerformed(ActionEvent evt) {
    	Date startDate = startDateControl.getDate();
    	Date endDate = endDateControl.getDate();
    	if(startDate != null && endDate != null){ 
    		//update bar
    		DefaultCategoryDataset bar = new DefaultCategoryDataset();
    		DefaultPieDataset pie = new DefaultPieDataset();
    		Iterator<TagUsage> it = StatisticHelper.getTagsUsage(startDate, endDate).iterator();
            while(it.hasNext()){
            	TagUsage e = it.next();
            	bar.addValue(e.getUsage(), e.getName(), "Tag");
            	pie.setValue(e.getName(), e.getUsage());
            }        
    		
    		//update pie
            ((CategoryPlot) chart1.getPlot()).setDataset(bar);
            ((PiePlot)chart2.getPlot()).setDataset(pie);
    		//update timeline
    		DateAxis axis = (DateAxis)chart3.getXYPlot().getDomainAxis();
    		axis.setRange(startDate, endDate);
    		//chartPanel3.getChart().getXYPlot().setDataset(0, new TimeSeriesCollection(StatisticHelper.getImageSeries(startDate, endDate)));
    	}
    }
    public void updateCharts(String userid)
    {
    	//update bar
		DefaultCategoryDataset bar = new DefaultCategoryDataset();
		DefaultPieDataset pie = new DefaultPieDataset();
		Iterator<TagUsage> it = StatisticHelper.getTagsUsage(userid).iterator();
        while(it.hasNext()){
        	TagUsage e = it.next();
        	bar.addValue(e.getUsage(), e.getName(), "Tag");
        	pie.setValue(e.getName(), e.getUsage());
        }        
		
		//update pie
        ((CategoryPlot) chart1.getPlot()).setDataset(bar);
        ((PiePlot)chart2.getPlot()).setDataset(pie);
		//update timeline
		DateAxis axis = (DateAxis)chart3.getXYPlot().getDomainAxis();
		//axis.setRange(startDate, endDate);
    }

	
}


